package com.teach.shape4;

import java.util.Scanner;

/**
 * 图形管理入口类
 *
 * @author J.L.Zhou
 *	jlzhou.top 版权所有
 */
public class ShapeMain {
	private static IShape[] list = new IShape[0];

	public static void main(String[] args) {
		while(true){
			
			
			
			int menu = selectMenu();
			
			if(menu==0){
				break;
			}
			
			IShape shape = null;
			switch (menu) {
			case IShape.RECTANGLE:
				shape = new Rectangle();
				break;
			case IShape.TRILATERAL:
				shape= new Trilateral();
				break;
			case IShape.ELLIPSE:
				shape = new Ellipse();
				break;
			case IShape.ROUNDNESS:
				shape = new Roundness();
				break;
			case IShape.SQUARE:
				shape = new Square();
				break;
			case 6:
				showList();
				break;
			case 7:
				order7();
				break;
			case 8:
				order8();
				break;
			case 9:
				order9();
				break;
	
			default:
				System.out.println("输入菜单编号错误，请重新输入");
				break;
			}
			
			if(shape!=null){
				shape.init();
				shape.show();
				add(shape);
			}
			
		}
		
	}
	
	/**
	 * 选择菜单
	 */
	public static int selectMenu() {
		System.out.println("---------菜单----------");
		System.out.println(" 1. 长方形");
		System.out.println(" 2. 三角形");
		System.out.println(" 3. 椭圆形");
		System.out.println(" 4. 圆形");
		System.out.println(" 5. 正方形");
		System.out.println(" 6. 显示输入的所有图形");
		System.out.println(" 7. 随机输出所有图形");
		System.out.println(" 8. 按面积从小到大排序输出图形");
		System.out.println(" 9. 按周长从大到小排序输出图形");
		System.out.println(" 0. 退出");
		System.out.println("----------------------");
		System.out.println("请输入菜单编号：");
		Scanner sc = new Scanner(System.in);
		return sc.nextInt();
	}
	
	/**
	 * 将图形加入数组中
	 * @param shape
	 */
	public static void add(IShape shape){
		IShape[] newList = new IShape[list.length+1];
		System.arraycopy(list, 0, newList, 0, list.length);
		newList[list.length]=shape;
		list = newList;
	}
	
	/**
	 * 显示输入的图形
	 */
	public static void showList(){
		if(list==null || list.length==0){
			System.out.println("尚未输入图形，请先输入");
			return ;
		}
		System.out.println("----------------显示图形----------------");
		for(IShape shape : list){
			shape.show();
		}
	}
	
	/**
	 * 随机输出所有图形
	 */
	public static void order7(){
		
	}
	
	/**
	 * 按面积从小到大排序输出图形
	 */
	public static void order8(){
		IShape temp;
		for (int j = 0; j < list.length - 1; j++) {
			for (int i = 0; i < list.length - 1 - j; i++) {
				if (list[i].getArea() > list[i + 1].getArea()) {
					temp = list[i];
					list[i] = list[i + 1];
					list[i + 1] = temp;
				}
			}
		}
		showList();
	}
	/**
	 * 按周长从大到小排序输出图形
	 */
	public static void order9(){
		
	}
}
